package com.lihan.GetSeatInLib.Controllers;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lihan.GetSeatInLib.Domain.FloorModel;
import com.lihan.GetSeatInLib.Domain.LibTable;
import com.lihan.GetSeatInLib.Domain.SeatModel;
import com.lihan.GetSeatInLib.Domain.StandReturnMessage;
import com.lihan.GetSeatInLib.Domain.Enums.SeatStaus;
import com.lihan.GetSeatInLib.Domain.Enums.TableStatus;
import com.lihan.GetSeatInLib.Exceptions.SeatNumOverException;
import com.lihan.GetSeatInLib.Service.FloorService;
import com.lihan.GetSeatInLib.Service.LibTableService;
import com.lihan.GetSeatInLib.Service.SeatService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @ClassName: LibTableController
 * @Description: 关于Table操作的Controller 在此Controller里不设置下属的Seat关系，关系在SeatController里维护
 * @author ASUS
 * @date 2022年3月3日
 *
 */
@Api(tags = "桌子操作Controller")
@RestController()
@RequestMapping("/Table")
public class LibTableController {
	
	@Autowired
	private LibTableService _tableService;
	
	@Autowired
	private FloorService _floorService;
	
	@Autowired
	private SeatService _seatService;
	
	
	@ApiOperation("查询所有的Table信息")
	@GetMapping("/GetAllTables")
	public StandReturnMessage<Set<LibTable>> GetAllTables(){
		Set<LibTable> tables = _tableService.getAllLibTale();
		if(tables!=null)
			return new StandReturnMessage<Set<LibTable>>(200, tables, 1);
		else
			return new StandReturnMessage<Set<LibTable>>(400, null, 0);
	}
	
	/**
	 * 
	 * @Title: CreateNewTable
	 * @Description: 创建新的桌子
	 * @param @param TableIndex 桌子的编号
	 * @param @param Seat_nums 桌子座位的上限
	 * @return StandReturnMessage<Boolean> 返回类型
	 * @throws
	 */
	@ApiOperation("创建一个新的桌子")
	@RequestMapping(method = RequestMethod.POST,value = "/CreateTable")
 	public StandReturnMessage<Boolean> CreateNewTable(Integer FloorID,String TableIndex,Integer Seat_nums){
		//先查一下是不是有这个楼层
		if(_floorService.getFloorById(FloorID)==null)
			return new StandReturnMessage<Boolean>(400, null, 0);
		FloorModel Floor = _floorService.getFloorById(FloorID);
		
		//构建桌子，刚创建为上锁状态，且没有下属的座位
		LibTable newOne = new LibTable();
		newOne.setTableIndex(TableIndex);
		newOne.setSeat_Number(Seat_nums);
		newOne.setStatus(TableStatus.LOCKED);
		newOne.setBelongFloor(Floor);
		//newOne.setSeatlist(new HashSet<SeatModel>());
		
		Boolean result = _tableService.CreateNewTable(newOne);
		
		if(result)
			return new StandReturnMessage<Boolean>(200, result, 1);
		else
			return new StandReturnMessage<Boolean>(400, false, 0,"创建桌子错误");
	}

	/**
	 * 
	 * @Title: UpdateTableBasicInfo
	 * @Description: 更新桌子的基本信息，注意----当修改座位个数时会进行已有的桌子数的检查
	 * @param @param ID      桌子ID
	 * @param @param TableIndex  桌子标号
	 * @param @param Seat_nums   卓伟个数
	 * @param @return 参数
	 * @return StandReturnMessage<Boolean> 返回类型
	 * @throws Exception 
	 * @throws
	 */
	@ApiOperation("更新一个桌子")
	@PostMapping("/UpdateTable")
	public StandReturnMessage<Boolean> UpdateTableBasicInfo(Long ID,String TableIndex,int Seat_nums) throws Exception{
	
		 LibTable table = _tableService.getTibTableById(ID);
		 table.setTableIndex(TableIndex);
		 table.setSeat_Number(Seat_nums);
		 if(table.getSeatlist().size()>Seat_nums) {
			 throw new Exception("现有的桌子个数已经超过设置的个数");
		 }
		 table.setSeat_Number(Seat_nums);
		 
		 Boolean result =  _tableService.UpdateLibTable(table);
		 if(result)
			 return new StandReturnMessage<Boolean>(200, result, 1);
		 else
		 	 return new StandReturnMessage<Boolean>(400, result, 0,"添加出错");
	}

	/**
	 * 
	 * @Title: CreateSeatInTable
	 * @Description: 在指定桌子下增加一个位置
	 * @param @param TableID  桌子ID
	 * @param @param IndexNum 座位标签
	 * @param @return
	 * @param @throws SeatNumOverException 参数
	 * @return StandReturnMessage<Boolean> 返回类型
	 * @throws
	 */
	@ApiOperation("在当前座子上新增一个位置")
	@GetMapping("/NewSeatInTable")
	public StandReturnMessage<Boolean> CreateSeatInTable(Long TableID,String IndexNum) throws SeatNumOverException{
		LibTable table = _tableService.getTibTableById(TableID);
		
		SeatModel newSeat = new SeatModel();
		newSeat.setIndexNum(IndexNum);
		newSeat.setStatus(SeatStaus.LOCKED);
		newSeat.setBelong(table);
		table.getSeatlist().add(newSeat);
		
		boolean result =  _tableService.UpdateLibTable(table);
		if(result)
			return new StandReturnMessage<Boolean>(200, result, 1);
		else
			return new StandReturnMessage<Boolean>(400, result, 0,"书桌新增座位出错");
	}

	/**
	 * 
	 * @Title: DeleteSeatInTable
	 * @Description: 在指定桌子下增加一个位置
	 * @param @param TableID 桌子ID
	 * @param @param SeatID 座位标签
	 * @param @return
	 * @param @throws SeatNumOverException 参数
	 * @return StandReturnMessage<Boolean> 返回类型
	 * @throws
	 */
	@ApiOperation("在当前座子上删除一个位置")
	@GetMapping("/DeleteSeatInTable")
	public StandReturnMessage<Boolean> DeleteSeatInTable(Long TableID,Long SeatID) throws SeatNumOverException{
		LibTable table = _tableService.getTibTableById(TableID);
		table.getSeatlist().forEach(item->{
			if(SeatID==item.getID())
				table.getSeatlist().remove(item);
		});
		boolean result =  _tableService.UpdateLibTable(table);
		if(result)
			return new StandReturnMessage<Boolean>(200,result,1);
		else
			return new StandReturnMessage<Boolean>(400,result,0,"书桌中座位删除出错");
	}

	/**
	 * 
	 * @Title: GetAllSeatInTable
	 * @Description: 获取当前桌子的所有桌位
	 * @param @param ID
	 * @param @return 参数
	 * @return StandReturnMessage<Set<SeatModel>> 返回类型
	 * @throws
	 */
	@ApiOperation("获取指定桌子下的座位")
	@GetMapping("/GetSeatsInTable")
	public StandReturnMessage<Set<SeatModel>> GetAllSeatInTable(Long ID){
		LibTable tab =  _tableService.getTibTableById(ID);
		return new StandReturnMessage<Set<SeatModel>>(200, tab.getSeatlist(), 1);
	}

	@ApiOperation("将其中的座位也连带锁定，（不锁定使用中的状态USED --这个用定时任务去隔断时间检查一下，再修正状态）")
	@GetMapping("/LockTable")
    public StandReturnMessage<Boolean> LockTable(Long ID) throws SeatNumOverException{
    	LibTable table = _tableService.getTibTableById(ID);
    	_tableService.LockSeat(ID);
    	//将其中的座位也连带锁定，（不锁定使用中的状态USED --这个用定时任务去隔断时间检查一下，再修正状态）
    	table.getSeatlist().forEach(item->{
    		_seatService.LockSeat(item.getID());
    	});
    	
    	boolean result =  _tableService.UpdateLibTable(table);
    	
    	if(result)
    		return new StandReturnMessage<Boolean>(200, result, 1);
    	else
    		return new StandReturnMessage<Boolean>(400, result, 0,"锁定书桌出错");
    }
    
	@ApiOperation("解锁一个桌子，并将下面的座位也解锁")
	@GetMapping("/UnLockTable")
    public StandReturnMessage<Boolean> UnLockTable(Long ID) throws SeatNumOverException{
    	LibTable table = _tableService.getTibTableById(ID);
    	_tableService.UnLockSeat(ID);
    	//将其中的座位也连带锁定，（不锁定使用中的状态USED --这个用定时任务去隔断时间检查一下，再修正状态）
    	table.getSeatlist().forEach(item->{
    		_seatService.UnLockSeat(item.getID());
    	});
    	
    	boolean result =  _tableService.UpdateLibTable(table);
    	if(result)
    		return new StandReturnMessage<Boolean>(200, result, 1);
    	else
    		return new StandReturnMessage<Boolean>(400, result, 0,"书桌解锁出错");
    }
}
